﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BaBaoFan.Modifier.Service;
using Microsoft.AspNetCore.Mvc;
using BaBaoFan.Modifier.Model;
using BaBaoFan.Modifier.Models;
using AutoMapper;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.Extensions.Caching.Memory;

namespace BaBaoFan.Modifier.Controllers
{
    public class CoinInfoController : BaseController
    {
        private readonly ICoinInfoService _coinInfoService;
        private readonly IAlgorithmService _algorithmService;
        private readonly IMapper _mapper;
        public CoinInfoController(ICoinInfoService coinInfoService, IAlgorithmService algorithmService, IMapper mapper, IMemoryCache memoryCache) : base(memoryCache)
        {
            _coinInfoService = coinInfoService;
            _algorithmService = algorithmService;
            _mapper = mapper;

        }
        public IActionResult List()
        {
            var coinInfos = _coinInfoService.GetCoinInfos();
            return View(coinInfos);
        }

        public IActionResult Delete(int[] ids)
        {
            _coinInfoService.Delete(ids);
            return Json("ok");
        }
        [HttpGet]
        public IActionResult Add()
        {
            CoinInfoCreareOrUpdateViewModel model = new CoinInfoCreareOrUpdateViewModel();
            model.AvaliableAlgorithms = _algorithmService.GetCoinAlgorithms().Select(x => new SelectListItem { Text = x.Algorithm, Value = x.Id.ToString() });
            return View(model);
        }
        [HttpPost]
        public IActionResult Add(CoinInfoCreareOrUpdateViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return View(vm);
            }
            var model = _mapper.Map<CoinInfo>(vm);
            _coinInfoService.Add(model);
            return Redirect("/CoinInfo/List");
        }

        [HttpPost]
        public IActionResult Search(string algorithm, int page = 1, int limit = 10)
        {
            var infoCondition = new CoinInfoCondition
            {
                Algorithm = algorithm,
                PageIndex = page,
                PageSize = limit,
                TotalCount = 0
            };
            var model = _coinInfoService.Search(infoCondition);
            return Json(new { code = 0, count = infoCondition.TotalCount, msg = "", data = model });
        }
        [HttpGet]
        public IActionResult Edit(int id)
        {
            var coinInfo = _coinInfoService.GetCoinInfoById(id);
            var vm = _mapper.Map<CoinInfoCreareOrUpdateViewModel>(coinInfo);
            vm.AvaliableAlgorithms = _algorithmService.GetCoinAlgorithms().Select(x => new SelectListItem { Text = x.Algorithm, Value = x.Id.ToString() });
            return View(vm);
        }
        [HttpPost]
        public IActionResult Update(CoinInfoCreareOrUpdateViewModel vm)
        {
            if (!ModelState.IsValid)
            {
                return View(vm);
            }
            var model = _mapper.Map<CoinInfo>(vm);
            _coinInfoService.Update(model);
            return Redirect("/CoinInfo/List");
        }

    }
}